1 Learning objectives

  1. You can identify and use the following tabs in RStudio: Source, Console, Environment, History, Files, Plots, Packages, Help and Viewer.

  2. You can modify RStudio’s interface options to suit your needs.

2 Introduction

Now that you have access to R & RStudio, let’s go on a quick tour of the RStudio interface, your digital home for the next few weeks.

First open RStudio either on the cloud or on your computer.

3 The RStudio panes

By default, RStudio is arranged into four window panes.

If you only see three panes, open a new script with File > New File > R Script . This should reveal one more pane.

Below we outline the four RStudio panes:

3.1 Source/Editor

The source pane or editor is where your R ‘scripts’ go. A script is a text document where you write and save code.

Open a new script under the File menu if you have not yet done so: File > New File > R Script. Here, type the following:

print("excited for R!")

To run code, place your cursor anywhere in the code, then hit Command + Enter on macOS, or Control + Enter on Windows.

This should send the code to the Console and run it.


You can also run multiple lines at once. To try this, type the following:

print("excited for R!")
print("and RStudio!")

Now drag your cursor to highlight both lines and press Command/Control + Enter.

There is also a ‘Run’ button at the top right of the source panel ( ), with which you can run code (either the current line, or all highlighted code). But you should try to use the keyboard shortcut instead.

To run the entire script, use Command/Control + A to select all code, then press Command/Control + Enter.


To open the script in a new window, click on the ( ) icon in the toolbar directly above the script.

To put the window back, click on the same button on the now external window.


Next, save the script. Hit Command/Control + S to bring up the Save dialog box. Save it with the name “rstudio_intro” in an easy-to-locate part of your computer, perhaps your desktop.


You can view data frames (which are like spreadsheets in R) in the same source pane. To observe this, type and run the code below.

View(cars)

Notice the uppercase V in View().

cars is the name of a dataset that comes loaded with R. It shows the speed of cars and the distances taken to stop, as observed in a study from the 1920s.

You can click on the “x” icon beside the tab name to close it.

3.2 Console

The console, at the bottom left, is where code is executed. You can type code directly here, but it will not be saved.

Type a random piece of code (maybe a calculation) and press ‘Enter’.

If you place your cursor on the last line of the console, and you press the up arrow, you can go back to the last code that was run. Keep pressing it to cycle to the previous lines.

To run any of these previous lines, just press Enter.

3.3 Environment, History and Tutorials

The Environment tab shows datasets and other objects that are loaded into R’s working memory, or “workspace”.

To explore this tab, let’s import a dataset into your workspace from the web. Type the code below into your script and run it:

ebola_data <- read.csv("https://tinyurl.com/ebola-data-sample")

You don’t need to understand exactly what the code is doing for now. We just want to quickly show you the basic features of the Environment pane; we’ll look at data importing in detail later.

Also, if you do not have active internet access, the code above will not run. You can skip this section and move to the “History” tab.

Now that the dataset is loaded, you can click on the blue drop-down icon beside the object’s name in the Environment tab to reveal a summary.

Clicking directly on the dataset opens it in a ‘View’ tab.

You can remove an object from the workspace with the rm() function. Type and run the following in a new line on your R script.

rm(ebola_data)

The broom icon, at the top of the Environment pane can also be used to clear your workspace.

Try re-running the line


The History tab shows previous commands you have run.


You can use Shift-click to select multiple lines. Click the first item you want to select to highlight it, then scroll to the last item you want to select, hold down the “Shift” key and click that item.

Now you can send this to the Console or the Source using the icons at the top of the History pane.


The Tutorials tab gives you access to some interactive R tutorials. You can ignore it for now.

If you are on Rstudio.cloud, the Connections and Git tabs may also be visible. These can also be ignored for now.

3.4 Files, Plots, Packages, Help and Viewer

Now, let’s take a look at the tabs in the bottom right pane.

First up, the Files tab. This shows the files and folders in the directory you are working in.

You can use this tab to look through our course repo.

Try playing with all the icons and buttons here, to see what they do.


Next, the Plots tab. This is where your plots show up. Try generating a simple plot with the following code:

plot(women)

Play with the icons at the top of this tab to explore what they do. In particular, you should explore how to export a plot.


Next, the Packages tab shows what packages or libraries are installed on your device, and allows you to install and load new packages.

But it is better to install and load packages with R code, rather than the Packages tab. Let’s try this. Type and run the code below to install the {highcharter} package.

The first line installs the package. The second line loads the package.

install.packages("highcharter")
library(highcharter)

Note that you only ever need to install a package once. But you need to load it with library() each time you start a new R session.


Fourth, the Help tab shows the documentation for different R objects. Try typing out and running each line below to see what this documentation looks like:

?print
?cars

Help files are not always very easy to understand for beginners, but with time they will become more useful.


Finally, the Viewer tab will allow you to preview HTML files and interactive objects. Type and run the code below to make an interactive histogram showing the speed of cars in the cars dataset.

hchart(cars$speed)

4 RStudio options

RStudio has a number of options for changing it’s look and functionality. Let’s try these now.

Open Tools > Global Options to bring up RStudio’s options menu. Then:

5 Wrapping up

Congratulations! You are now a new citizen of RStudio.

Of course, you have only scratched the surface of RStudio functionality. As you advance in your R journey, you will discover new features, and you will hopefully grow to love the wonderful integrated development environment (IDE) that is RStudio. One good place to start is the official RStudio IDE cheatsheet.

Below is one section of that sheet:

See you in the next lesson!

6 Further resources

  1. 23 RStudio Tips, Tricks, and Shortcuts

Contributors

The following team members contributed to this lesson:

7 References

Some material in this lesson was adapted from the following sources:

This work is licensed under the Creative Commons Attribution Share Alike license. Creative Commons License